package com.ford.trade.processor;

import com.ford.trade.entity.AoKeGame;
import com.ford.trade.service.AoKeGameService;
import lombok.Data;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class AoKeTeamProcessor implements PageProcessor {

    private AoKeGameService gameService;

    private String url;
    private String week;
    private String getResult;

    @Override
    public void process(Page page) {
        Document document = page.getHtml().getDocument();
        Element elementById = document.getElementById("table" + week);
        List<Element> elements = new ArrayList<>();
        elements.addAll(elementById.getElementsByClass("alltrObj trClassbg endMacthObj"));
        elements.addAll(elementById.getElementsByClass("alltrObj endMacthObj"));
        elements.addAll(elementById.getElementsByClass("alltrObj trClassbg"));
        elements.addAll(elementById.getElementsByClass("alltrObj"));
        for (Element element : elements) {
            AoKeGame game = new AoKeGame();
            String attr = element.getElementsByClass("tdfx td8 textnowrap")
                    .first().getElementsByTag("a")
                    .first().attr("href");
            //         /soccer/match/1157630/odds/

            String matchid = attr.substring(attr.indexOf('h') + 2, attr.length() - 6);
            game.setIndexId(Long.valueOf(matchid));

            //type
            String a = element.getElementsByTag("a").first().text();
            game.setType(a);
            String datetime = element.getElementsByClass("switchtime timetd td2").attr("title");
            datetime = datetime.substring(datetime.indexOf("：") + 1);
            game.setDate(datetime);

            game.setZhuDui(element.getElementsByClass("homenameobj homename").text());
            String rangQiuZhi = element.getElementsByClass("handicapobj font_red").text();
            if ("(+1)".equals(rangQiuZhi)) {
                game.setRangQiu(1);
            } else if ("(-1)".equals(rangQiuZhi)) {
                game.setRangQiu(-1);
            } else {
                game.setRangQiu(0);
            }
            game.setWeek(week);
            game.setKeDui(element.getElementsByClass("awaynameobj awayname").text());
            game.setResult(element.getElementsByClass("tdfx td6").text());
            gameService.save(game);
        }
    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
            .addCookie("cookie", "myWDLDataObj=get_oupan%7C0%231%2Cget_oupan%7C2%230%2Cget_yapan%7C5%230; myWDLDataObj=get_oupan%7C0%231%2Cget_oupan%7C2%230%2Cget_yapan%7C5%230; LastUrl=; __utmc=56961525; __utmz=56961525.1653290495.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; BAIDU_SSP_lcr=https://www.baidu.com/link?url=SStKhMhziiQVseuA1E9Z3SpPEKPbhE5wJfvXcIirKfm&wd=&eqid=d207e11c000151cb00000004628b37a4; Hm_lvt_213d524a1d07274f17dfa17b79db318f=1652765523,1652862617,1652921906,1653290495; PHPSESSID=49140db8cad3f6d59ee8612b9bd82ddecc02b0ab; pm=; FirstURL=www.okooo.com/; FirstOKURL=https%3A//www.okooo.com/danchang/; First_Source=www.okooo.com; IMUserID=31747680; IMUserName=%E9%A3%9E%E9%B8%9F%E9%9B%B7%E9%B8%A3; OKSID=49140db8cad3f6d59ee8612b9bd82ddecc02b0ab; M_UserName=%22%5Cu98de%5Cu9e1f%5Cu96f7%5Cu9e23%22; M_UserID=31747680; M_Ukey=39755d37eb1ef335ded3a5b5f523199c; OkAutoUuid=a8a9a29ce8bbd1db3b12c2d1c00b91e0; OkMsIndex=5; data_start_isShow=1; __utma=56961525.267670634.1653290495.1653291754.1653380750.4; acw_tc=2f624a1e16533811782431430e4610a1068f8c3547ccdb741f4d1cce659d9e; Hm_lpvt_213d524a1d07274f17dfa17b79db318f=1653380906; __utmb=56961525.28.8.1653380907037")
            .setTimeOut(3000)
            .setRetrySleepTime(1000)
            .setRetryTimes(1);

    @Override
    public Site getSite() {
        return site;
    }

}
